package com.aote.expression.paramprocessor;

import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.w3c.dom.Element;

import com.aote.entity.EntityType;
import com.aote.entity.EntityTypeFactory;
import com.aote.expression.Param;
import com.browsesoft.Entity;
import com.browsesoft.SequenceFind;
import com.browsesoft.baseadapter.AdvancedSimpleEntity;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.htmlcomponent.HTMLBasicModel;

public class AmountByGroupProcessor extends MultiValueParamProcessor {

	public Collection process(Param param, Map attr, HTMLBasicComponent comp,
			Element element) throws NoFitValueException {
		Map map = param.getParams();
		// 实体类型
		String entityType = element.getAttribute("entitytype");
		// 子列表名
		String attrName = (String) map.get("attrname");
		// 分组依据的字段
		String groupField = (String) map.get("groupfield");
		// 求和的字段
		String sumField = (String) map.get("sumfield");
		HTMLBasicModel model = comp.getModel();
		if (!(model instanceof AdvancedSimpleEntity)) {
			return null;
		}
		String parentid = ((AdvancedSimpleEntity) model).getID();
		Map groups = new HashMap();
		// 所有子实体
		List entities = ((AdvancedSimpleEntity) model)
				.getPartEntities(attrName);
		if (entities == null) {
			return null;
		}
		Iterator iter = entities.iterator();
		while (iter.hasNext()) {
			// 找到分组字段的所有值
			Entity ent = (Entity) iter.next();
			String str = (String) ent.getAttributes().get(groupField);
			if (str == null || str.equals("")) {
				continue;
			}
			// 如果有分组字段，将求和字段的值与本组的和值相加
			String sumValue = (String) ent.getAttributes().get(sumField);
			if (groups.containsKey(str)) {
				double value = Double.parseDouble((String) groups.get(str))
						+ Double.parseDouble(sumValue);
				groups.put(str, String.valueOf(value));
			} else {
				groups.put(str, sumValue);
			}
		}
		// 父的实体类型
		String parenttype = comp.getElement().getAttribute("entitytype");
		List result = this.getDatas(entityType, groupField, sumField, parentid,
				groups, parenttype);
		return result;
	}

	/**
	 * 将得到的数据组织成hashtable属性，然后根据此hashtable属性构造AdvanceSimpleEntity
	 * 
	 * @param entityType
	 * @param groupField
	 * @param sumField
	 * @param parentid
	 * @param groups
	 * @return
	 */
	private List getDatas(String entityType, String groupField,
			String sumField, String parentid, Map groups, String parenttype) {
		try {
			List result = new LinkedList();
			Iterator iter = groups.entrySet().iterator();
			while (iter.hasNext()) {
				Map.Entry entry = (Map.Entry) iter.next();
				Hashtable attributes = new Hashtable();
				String id = Integer.toString(SequenceFind.getID());
				attributes.put("id", id);
				String selfid = Integer.toString(SequenceFind.getID());
				attributes.put("selfid", selfid);
				attributes.put("parenttype", parenttype);
				attributes.put(groupField, (String) entry.getKey());
				attributes.put(sumField, (String) entry.getValue());
				attributes.put("parentid", parentid);
				attributes.put("entitytype", entityType);
				// 放表名
				EntityType type = EntityTypeFactory.getInstance()
						.getEntityType(entityType);
				String tableName = type.getTableName();
				attributes.put("tablename", tableName);
				// 根据attributes构造实体
				AdvancedSimpleEntity ase = new AdvancedSimpleEntity(attributes);
				result.add(ase);
			}
			return result;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
